package csse3004.GD.PM;

import csse3004.GD.DA.PollMasterDA;
import java.util.ArrayList;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CustomDemoController extends SimpleFormController {
    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());
    private PollMasterService pms = new PollMasterDA();
    
    //TODO @EVERYONE Who wrote this class
    
    public ModelAndView onSubmit(Object command) throws ServletException {
        
        CustomDemo createDemo = (CustomDemo)command;
        String demo = createDemo.getDemographic();
        String demoMsg = "Demographic has been successfully added!";
        int tempID = createDemo.getTemplateID();            
        
        try {
            if (!demo.isEmpty()) {
                ArrayList<String> arr = new ArrayList<String>();
                arr.add(demo);
                //int userID = Integer.parseInt((String)req.getSession().getAttribute("userID"));
                logger.info("Temp ID:" + tempID);
                logger.info("Demographic added is: " + demo);

                pms.addDemographics(tempID, arr); 
                //TODO @EVERYONE this function doesn't seem to be correct, as it doesn't reference a template

                logger.info(demoMsg);

            }   
            return new ModelAndView("successMsg", "demoMsg", demoMsg);
        } catch (Exception e) {
            logger.info(e);
            return new ModelAndView("customDemo");
        }
    }
}
